Namespaces

One of the newest and often misunderstood features of XML is that of namespaces.  If you have ever programmed in a language like C++ or Java, you may already have come across namespaces.  All namespaces do is help you differentiate elements that may be in conflict.  The easiest way to understand namespaces is to see an example.

Let’s say that you want to create a document that compares prices for your merchandize to those of another company.  You will probably have a <price> element, and so will they.  When you merge the two, you will have two <price> elements.  This sounds reasonable, but which price is yours?  Take a look at the following example XML document.

<?xml version="1.0"?>

<products xmlns:mine="http://www.mine.com"
	  xmlns:theirs="http://www.theirs.com">

	<mine:product>
		<mine:name>The Juice Tiger</mine:name>
		<mine:price>$49.95</mine:price>
		<theirs:price>$54.95</theirs:price>
	</mine:product>

	<mine:product>
		<mine:name>The Salad Shooter</mine:name>
		<mine:price>$35.95</mine:price>
		<theirs:price>$38.95</theirs:price>
	</mine:product>

</products>
File: Chapter 3\Namespaces.xml

Let’s break this document down.  First, let’s look at the declaration of the two namespaces:

<products 	xmlns:mine="http://www.mine.com"
	  		xmlns:theirs="http://www.theirs.com">

The attribute “xmlns” (XML namespace) is used to define a namespace name.  To create a name space, type “xmlns:” then the name of the namespace.  Then type a URI that is associated with the namespace.  What does the URI point to?  Nothing!  It does not go out to the network or the Internet and do anything.  It acts as the differentiator between namespaces.  

Tip:	Do not worry about what the URI in the namespace declaration does.  It doesn’t do anything.  The standards committee for XML chose URI’s because they figured that they would be globally unique.

Now we simply use the appropriate namespace as we declare our tags.

<mine:price>$35.95</mine:price>
<theirs:price>$38.95</theirs:price>

Even though there are two price elements, we can use the appropriate namespace in the tag definition so that there can be a clear distinction.

Making Documents with Namespaces More Readable

As you look at the example document, it may seem rather busy.  You can make your documents that use namespaces more readable by declaring a default namespace.  A default namespace uses the “xmlns” attribute without any further name.  All elements that are declared without an explicit namespace in the tag name are assumed to be part of the default namespace.  Using a default namespace, look at how much more readable the same document is:

<?xml version="1.0"?>

<products xmlns="http://www.mine.com"
	  xmlns:theirs="http://www.theirs.com">
	<product>
		<name>The Juice Tiger</name>
		<price>$49.95</price>
		<theirs:price>$54.95</theirs:price>
	</product>

	<product>
		<name>The Salad Shooter</name>
		<price>$35.95</price>
		<theirs:price>$38.95</theirs:price>
	</product>
</products>

File: Chapter 3\NamespacesWithDefault.xml

The new document only has to explicitly specify a namespace when it using one other than the default.

As you can see, there is no magic to namespaces.  They are very effective and are being used more and more in the XML community.  Do not be surprised if you see them in the majority of the XML documents that you encounter.